package com.jeasonzhao.commons.excel;

import java.lang.reflect.Field;

import org.apache.poi.hssf.util.HSSFColor;
import com.jeasonzhao.commons.utils.ConvertEx;

public class ColorHelper
{
    private static java.util.Hashtable<String,String> m_mapInt2HexColor = null;
    private static java.util.Hashtable<String,Integer> m_mapString2ColorIndex = null;
    private ColorHelper()
    {
    }

    private synchronized static void init()
    {
        if(null != m_mapInt2HexColor)
        {
            return;
        }
        m_mapInt2HexColor = new java.util.Hashtable<String,String>();
        m_mapString2ColorIndex = new java.util.Hashtable<String,Integer>();
        Class<?> [] classes = HSSFColor.class.getClasses();
        for(int n = 0;n < classes.length;n++)
        {
            Class<?> cls = classes[n];
            if(HSSFColor.class.isAssignableFrom(cls) == false)
            {
                continue;
            }
            int nx = cls.getName().indexOf("$");
            if(nx < 0)
            {
                continue;
            }
            try
			{
				String strName = cls.getName().substring(nx + 1).toLowerCase();
				Field f1 = cls.getField("index");
				Field f2 = cls.getField("hexString"); //"3333:CCCC:CCCC";
				int nIndex = ConvertEx.toInt(f1.get(null));
				String strHex = shrinkHex((String) f2.get(null));
				m_mapInt2HexColor.put("COLOR" + nIndex,strHex);
				m_mapInt2HexColor.put(strName,strHex);
				m_mapString2ColorIndex.put(strName,Integer.valueOf(nIndex));
			}
			catch (NumberFormatException e)
			{
				continue;
			}
			catch (SecurityException e)
			{
				continue;
			}
			catch (NullPointerException e)
			{
				continue;
			}
			catch (IllegalArgumentException e)
			{
				continue;
			}
			catch (NoSuchFieldException e)
			{
				continue;
			}
			catch (IllegalAccessException e)
			{
				continue;
			}
        }
    }

    public static short getIndex(String str)
    {
        if(null == str)
        {
            return 0;
        }
        init();
        String strKey = str.trim().toLowerCase();
        if(m_mapString2ColorIndex.containsKey(strKey))
        {
            Integer in = (Integer) m_mapString2ColorIndex.get(strKey);
            return(short) in.intValue();
        }
        else
        {
            return 0;
        }
    }

    private static String shrinkHex(String strHex)
    {
        if(null == strHex)
        {
            return null;
        }
        if(strHex.length() > 0)
        {
            String[] a = strHex.split(":");
            StringBuffer strHexNew = new StringBuffer();
            for(int n = 0;n < a.length;n++)
            {
                if(a[n].length() > 0)
                {
                    if(a[n].length() < 2)
                    {
                        strHexNew.append("0" + a[n]);
                    }
                    else

                    {
                        strHexNew.append(a[n].substring(0,2));
                    }
                }
            }
            return strHexNew.toString();
        }
        return null;
    }

    public static String getHex(HSSFColor c)
    {
        return shrinkHex(c == null ? "" : c.getHexString());
    }

    public static String getHex(int nColorIndex)
    {
        init();
        String key = "COLOR" + nColorIndex;
        if(m_mapInt2HexColor.containsKey(key))
        {
            return(String) m_mapInt2HexColor.get(key);
        }
        else
        {
            return null;
        }
    }

    public static String getHex(String strClorName)
    {
        init();
        if(null == strClorName)
        {
            return null;
        }
        String strKey = strClorName.trim().toLowerCase();
        if(m_mapInt2HexColor.containsKey(strKey))
        {
            return(String) m_mapInt2HexColor.get(strKey);
        }
        else
        {
            return null;
        }
    }
}
